package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;


import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;


@Controller
@RequestMapping("/wtzycontroller")
public class wtzycontroller {
	private static final Logger logger = Logger.getLogger(wtzycontroller.class);
	Map map = new HashMap();
	List list = new ArrayList();
	
	@RequestMapping("/test")
	@ResponseBody
	public String ptdl(HttpServletResponse response, HttpSession session,HttpServletRequest request, String account, String password,
			String DXYZ) throws ClientProtocolException, IOException {
			SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		reqMap.put("AUTHORITY", "YBGJJ165");
		reqMap.put("OpenKey",sessionIn.getKEY());
		BODY b=new BODY();
		b.setZJHM(sessionIn.getZJHM());
		reqMap.put("BODY",b);
		OpenKey opk=new OpenKey();
		String result=opk.getOpenKey(reqMap, session);
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0);
				map.put("msg", "1");
				map.put("data", mx.get("RETDATA"));
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("msg", "1");
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
		}
	
		return null;
	}
	@RequestMapping("/test1")
	@ResponseBody
	public String entryinterface1(HttpServletRequest request, HttpServletResponse response,HttpSession session,
			String xm,String wtyh,String hkqsrq,String hkzzrq,String gsdqdm,String dqdm,String dkzh,
			String dwmc,String dwzh,String htbh,String dh,String zjhm,String jcyh,String yjce,
			String grzh,String ye,String HKKH,String YHKE) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		Map reqMap = new HashMap();
		reqMap.put("AUTHORITY", "YBGJJ166");
		try {
			if(hkqsrq !=null && !"".equals(hkqsrq)){
				b.setHKQSRQ(hkqsrq);	
			}else{
				b.setHKQSRQ("");
			}
			if(hkzzrq !=null && !"".equals(hkzzrq)){
				b.setHKJSRQ(hkzzrq);
			}else{
				b.setHKJSRQ("");
			}
			if(gsdqdm !=null && !"".equals(gsdqdm)){
				b.setGSDQMD(gsdqdm);
			}else{
				b.setGSDQMD("");
			}
			if(dqdm !=null && !"".equals(dqdm)){
				b.setDQDM(dqdm);
			}else{
				b.setDQDM("");
			}
			if(dkzh !=null && !"".equals(dkzh)){
				b.setDKZH(dkzh);
			}else{
				b.setDKZH("");
			}
			if(xm !=null && !"".equals(xm)){
				b.setXINGMING(xm);
			}else{
				b.setXINGMING("");
			}
			if(dh !=null && !"".equals(dh)){
				b.setDH(dh);
			}else{
				b.setDH("");
			}
			if(wtyh !=null && !"".equals(wtyh)){
				b.setWTYH(wtyh);
			}else{
				b.setWTYH("");
			}
			if(dwmc !=null && !"".equals(dwmc)){
				b.setDWMC(dwmc);
			}else{
				b.setDWMC("");
			}
			if(dwzh !=null && !"".equals(dwzh)){
				b.setDWZH(dwzh);
			}else{
				b.setDWZH("");
			}
			if(htbh !=null && !"".equals(htbh)){
				b.setHTBH(htbh);
			}else{
				b.setHTBH("");
			}
			if(zjhm !=null && !"".equals(zjhm)){
				b.setZJHM(zjhm);
			}else{
				b.setZJHM("");
			}
			if(jcyh !=null && !"".equals(jcyh)){
				b.setJCYH(jcyh);
			}else{
				b.setJCYH("");
			}
			if(yjce !=null && !"".equals(yjce)){
				b.setYJCE(yjce);
			}else{
				b.setYJCE("");
			}
			if(grzh !=null && !"".equals(grzh)){
				b.setGRZH(grzh);
			}else{
				b.setGRZH("");
			}
			if(ye !=null && !"".equals(ye)){
				b.setYE(ye);
			}else{
				b.setYE("");
			}
			if(HKKH !=null && !"".equals(HKKH)){
				b.setKAHAO(HKKH);
			}else{
				b.setKAHAO("");
			}
			if(YHKE !=null && !"".equals(YHKE)){
				b.setYHKE(YHKE);
			}else{
				b.setYHKE("");
			}
			b.setCZY("网厅");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "1");
			map.put("msg", "1");
			map.put("CW","参数缺失请确认您填写的数据是否准确");
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			String jsonString = JSONUtil.ToJsongString2(map);
			PrintWriter pw = response.getWriter();
			pw.write(jsonString);
			return null;
		}
		
		reqMap.put("OpenKey",sessionIn.getKEY());
		reqMap.put("BODY",b);
		String  result = opk.getOpenKey(reqMap, session);
		logger.info("YBGJJ166接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				map.put("code", 0);
				map.put("msg", "1");
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("msg", "1");
				map.put("code", 1);
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
		}
	
		return null;
	}
	/**
	 * 查询是否可以办理委托逐月
	 * @param request
	 * @param response
	 * @param limit
	 * @param id
	 * @param start
	 * @param end
	 * @param JKRXM
	 * @param session
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/test3")
	@ResponseBody
	public String pd(HttpServletRequest request, HttpServletResponse response , String limit ,String YWLSH,String start,String end,String JKRXM,HttpSession session) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ224");
		//接口参数
		reqMap.put("OpenKey",sessionIn.getKEY());
		BODY b=new BODY();
		b.setID(YWLSH);
		reqMap.put("BODY",b);
		OpenKey opk=new OpenKey();
		String result=opk.getOpenKey(reqMap, session);
		Map map  = new HashMap();
		logger.info("接口返回内容:"+result);
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0);
				map.put("msg", "1");
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("msg", "1");
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		return null;
	}
}
